/**
* 
* Data : contraintes fonctionnellle entre t??ches 
* Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN  
* 
* see <a href="{@docRoot}/Documentation/data.html"> for more information</a>
* 
* @author: Urunuela Richard
* @version 3.2
*/
package storm.Data;

import java.beans.XMLEncoder;

import storm.Entity;
import storm.KernelMessage;
import storm.Data.DataRule.AutoStart;
import storm.Data.DataRule.Icontrol;
import storm.KernelMessage.value;
import storm.Tasks.Task;



public class Data extends Entity {
	public Task source;
	public Task destination;
	int size=0;
	int rate=1; //Divide  period s for d 
	private int countBase=0;
	int idS=0;
	int idD=0;
	private boolean produite;
	private boolean consommer ;
	public Icontrol ControlRule;
	
	public Icontrol getControlRule() {
		return ControlRule;
	}
	public void setControlRule(Icontrol controlRule) {
		ControlRule = controlRule;
	}
	/**
	 * @deprecated 
	 */
	public String toXMl(   XMLEncoder e){
		String res="";
		return res;
	}
	/**
	 * 
	 */
	public Data(){
//		this.ControlRule = new Math();
	//	this.ControlRule = new Normal();
		this.ControlRule = new AutoStart();
	this.ControlRule.setData(this);
	}
	/**
	 * 
	 * @param S
	 * @param D
	 * @param s
	 */
	public Data(Task S,Task D,int s){
		this();
		this.setSource(S);
		this.setDestination(D);
		this.size=s;
		this.rate = 1;
		this.setCountBase(1);
		
	}
	/**
	 * 
	 * @param S
	 * @param D
	 * @param s
	 * @param c
	 */
	public Data(Task S,Task D,int s,int c){
		this();
		this.setSource(S);
		this.setDestination(D);
		this.size=s;
		this.rate = c;
		this.setCountBase(1);
		
	}
	
	/**
	 * Surchage raise block pour la gestion  des statisitique de produciton 
	 */
	public void raiseBlock(Entity A){
		//KernelMessage m = new KernelMessage(this,A,KernelMessage.value.BLOCK); 
		//this.Kernel.addMessage(m);
		if (this.ControlRule.hasToBlockDestination()) {
			debug_mess("[Raise Block  Task REcord message] "+ this ,10);	
			KernelMessage m = new KernelMessage(this,A,KernelMessage.value.BLOCK); 
			this.Kernel.addMessage(m);
			//System.out.println("bloc");
		}		
	}
	
	/**
	 * Voir a faire la m?me chose pour le raise unblock
	 */
	/**
	 * 
	 */
	public void onTick(){
	
		this.debug_mess("[DATA EVT TICK Calcul des contraintes ] de " + this.getSource() +  " pour " + this.getDestination()  + " compteur " + this.rate + " sur " + this.getCountBase(),10 );
		/**
		 * 1 - Detect end task 
		 */
		if (( this.getSource().getdataControlRunning() )) {
			this.debug_mess("[DATA DETECT END TASK Source] "  + this.getSource() + " " + this.rate + " " + this,10);
			this.setProduite(true);
			this.ControlRule.isProduct();
		//	this.getSource().dataControlRunning = false;
			/**
			 * ready to be consume if ratio is OK
			 */
		}
		if (( this.getDestination().getdataControlRunning() )) {
			this.debug_mess("[DATA DETECT END TASK DESTINATION] "  + this.getDestination() + " " + this.rate + " " + this,10);
			this.ControlRule.isConsume();
			this.setConsommer(true);
		//	this.getDestination().dataControlRunning = false;
			/**
			 * end consume 
			 */
		}
		
	}
	/**
	 * 
	 * @return
	 */
	public int getSize() {
		return size;
	}
	/**
	 * 
	 * @param size
	 */
	public void setSize(int size) {
		this.size = size;
	}
	/**
	 * 
	 * @return
	 */
	public int getRate() {
		return rate;
	}
	/**
	 * 
	 * @param count
	 */
	public void setRate(int count) {
		this.rate = count;
	}
	/**
	 * 
	 * @return
	 */
	public int getIdS() {
		return idS;
	}
	/**
	 * 
	 * @param idS
	 */
	public void setIdS(int idS) {
		this.idS = idS;
	}
	/**
	 * 
	 * @return
	 */
	public int getIdD() {
		return idD;
	}
	/**
	 * 
	 * @param idD
	 */
	public void setIdD(int idD) {
		this.idD = idD;
	}
	/**
	 * 
	 */
	public String getName(){
		return this.name;
	}
	/**
	 * 
	 * @param source
	 */
	public void setSource(Task source) {
		this.source = source;
	}
	/**
	 * 
	 * @return
	 */
	public Task getSource() {
		return source;
	}
	/**
	 * 
	 * @param destination
	 */
	public void setDestination(Task destination) {
		this.destination = destination;
	}
	/**
	 * 
	 * @return
	 */
	public Task getDestination() {
		return destination;
	}
	public void setProduite(boolean produite) {
		this.produite = produite;
	}
	public boolean isProduite() {
		//this.ControlRule.isProduct();
		if (this.ControlRule.isAvailableToDestination()){ 
				return produite;
		}
		else {
			return false;
		}
	}
	public void setCountBase(int countBase) {
		this.countBase = countBase;
	}
	public int getCountBase() {
		return countBase;
	}
	public void setConsommer(boolean consommer) {
		this.consommer = consommer;
	}
	public boolean isConsommer() {
		return consommer;
	}
	public void init(){
		
		super.init();
		this.ControlRule.init();
	}

}
